import { CommonUtil } from './../../../../common/utils/common.util';
import { CommentService } from './service/comment.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common'
import { element } from '@angular/core/src/render3/instructions';


@Component({
    selector: 'app-estate-comment',
    templateUrl: './estate-comment.component.html',
    styleUrls: ['./estate-comment.component.scss']
})
export class EstateCommentComponent implements OnInit {

    public loading: boolean = false;
    public error: boolean = false;
    public isOne: boolean = true;
    public projectId: any;
    public counTwo: any;
    public comments: any = [];
    public commentsTow: any = [];
    public tags: any;
    public topQuestion: any;
    public isTabTwoApi: boolean = false;
    public isActiveIndex = 0;
    tabs = ['楼盘', '户型']



    constructor(
        private location: Location,
        private router: Router,
        private service: CommentService,
        private routeInfo: ActivatedRoute,
        private commonUtil: CommonUtil,
    ) { }

    ngOnInit() {

        this.projectId = this.routeInfo.snapshot.queryParams['project'];

        this.tagService(res => {
            this.tabOne();
        });

    }

    reqFormate(Lambda, more?) {
        return {
            "pager": {
                "Start": 0,
                "Limit": 10
            },
            "filter": {
                "Lambda": Lambda + (more ? more : ""),
                "Navigations": ["LayoutInfo"], "Latitude": 28.692499,
                "Longitude": 115.815292
            }, "sorters": [{
                "Property": "item=>item.Top",
                "Direction": "Descending"
            }, {
                "Property": "item=>item.SubmitTime",
                "Direction": "Descending"
            }]
        }
    }

    tabOne(_req?) {

        let req = this.reqFormate("item=>item.Review&&item.Project==" + this.projectId, _req)

        this.queryCommentSummary(req, res => {

            this.loading = false;
            this.error = false;

            if (res.Result && res.Success) {

                let _ay = [];

                res.Result.Value.forEach(element => {

                    element['NickPhoto'] = "";

                    if (element.Nick.Photo) {
                        element.NickPhoto = this.commonUtil.getThumbnail(element.Nick.Photo);
                    }
                    if (element.Photo) {
                        element.Photo = this.commonUtil.getPhotoByName(element.Photo, 'Comment');
                    }

                    _ay.push(element);
                });

                this.comments = res.Result.Value;


                this.topQuestion = _ay.pop()["QuestionSummary"]['Content'];


            }

        });
    }


    tabTwo() {

        let req = this.reqFormate("item=>item.Review&&item.Layout!=null&&item.Project==" + this.projectId)

        this.queryCommentSummary(req, res => {

            this.loading = false;
            this.error = false;
            this.isTabTwoApi = true;

            this.counTwo = res.Result.Count;

            let _ay = [];

            res.Result.Value.forEach(element => {

                element['NickPhoto'] = "";
                element['LayoutPhoto'] = "";

                if (element.Nick.Photo) {
                    element.NickPhoto = this.commonUtil.getThumbnail(element.Nick.Photo);
                }
                if (element.Photo) {
                    element.Photo = this.commonUtil.getPhotoByName(element.Photo, 'Comment');
                }

                if (element.LayoutReference.Photo) {
                    element.LayoutPhoto = this.commonUtil.getPhotoByName(element.LayoutReference.Photo, '户型图');

                }
                _ay.push(element);
            });

            this.commentsTow = _ay;

        });
    }

    getTagDetail(name,index) {


        let _req = name ? '&&item.Tag.Contains("' + name + '")' : "";

        console.log(index);
        this.isActiveIndex = index;

        this.tabOne(_req);
    }

    praise(id) {
        this.service.praise({ "comment": id }, res => { });
    }

    onClickLeft() {
        this.location.back()
    }

    goDetail(index, type?) {
        this.router.navigate(['/workspace/estate/commentDetail'], {
            queryParams: {
                comment: index,
                type: type
            }
        });
    }

    onTabChange(e) {
        let index = e.index;
        if (index === 0) {
            this.isOne = true;
        } else {
            this.isOne = false;

            if (!this.isTabTwoApi) {

                this.tabTwo();
            }
        }

    }

    tagService(callback) {
        let req = {
            "project": this.projectId,
            "category": "TagCategory_House1_Comment",
            "limit": 100
        }

        // this.loading = true;
        this.service.tagService(req, res => {
            if (res.Result && res.Success) {
                this.tags = res.Result;
                // this.loading = false;
                callback();
            }
        }, error => {
            this.error = true;
        });
    }

    queryCommentSummary(req, callback) {

        this.loading = true;

        this.service.queryCommentSummary(req, res => {
            if (res.Result && res.Success) {
                callback(res);
            }

        }, error => {
            this.error = true;
        });
    }

}
